package com.android.questions.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;

public class BitmapUtil {

    /**
     * 把两个位图覆盖合成为一个位图，以底层位图的长宽为基准
     *
     * @param backBitmap  在底部的位图
     * @param frontBitmap 盖在上面的位图
     * @return bitmap
     */
    public static Bitmap mergeBitmap(Context context, Bitmap backBitmap, Bitmap frontBitmap) {
        if (backBitmap == null || backBitmap.isRecycled()
                || frontBitmap == null || frontBitmap.isRecycled()) {
//            Log.e("AAA", "backBitmap=" + backBitmap + "; frontBitmap=" + frontBitmap);
            return null;
        }
        Bitmap bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);
        float density = context.getResources().getDisplayMetrics().density;
//        Log.e("AAA", density + "");
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(frontBitmap, (int) (55 * density), (int) (55 * density), false);
        canvas.drawBitmap(scaledBitmap, 215 * density, 433 * density, null);
//        Log.e("AAA", "backBitmap.width=" + backBitmap.getWidth() + ";backBitmap.height=" + backBitmap.getHeight());
//        Log.e("AAA", "frontBitmap.width=" + frontBitmap.getWidth() + ";frontBitmap.height=" + frontBitmap.getHeight());
//        Log.e("AAA", "scaledBitmap.width=" + scaledBitmap.getWidth() + ";scaledBitmap.height=" + scaledBitmap.getHeight());
//        Log.e("AAA", "scaledBitmap.left=" + 160 * density + ";scaledBitmap.top=" + 326 * density);
        return bitmap;
    }
}